package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.globale.Globale;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam(name = "page") Integer page,
                              @RequestParam(name = "size") Integer size) {
        //分页条件
        Page<Item> itemPage = new Page<>(page, size);
        //查询条件
        LambdaQueryWrapper<Item> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //LambdaQueryWrapper<Item> wrapper = lambdaQueryWrapper.in(Item::getStatus, 1, 2);
        LambdaQueryWrapper<Item> wrapper = lambdaQueryWrapper.ne(Item::getStatus, 3);

        //分页查询结果
        Page<Item> result = itemService.page(itemPage,wrapper);
        //新建空的返回结果集
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        //设置返回结果
        itemPageDTO.setList(result.getRecords());
        itemPageDTO.setTotal(result.getTotal());

        return itemPageDTO;

    }

    /**
     * 添加
     *
     * @param item
     * @return
     */
    @PostMapping
    public String add(@RequestBody Item item) {

        /*if (StringUtils.) {
        }*/

        item.setUpdateTime(new Date());
        item.setCreateTime(new Date());
        item.setStatus(Globale.STATS_DEFAULT);
        itemService.save(item);
        log.info("添加成功");
        return "添加成功";
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item select(@PathVariable(name = "id") long id) {

        log.info("查询成功");
        return itemService.getById(id);
    }

    /**
     * 上下架
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public String setStatus(@PathVariable(name = "id") long id,
                            @PathVariable(name = "status") Integer status) {

        Item item = itemService.getById(id);
        item.setStatus(status);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        if (status == Globale.STATS_DEFAULT) {
            log.info("上架成功");
        } else {
            log.info("下架成功");
        }
        return "上/下架成功";
    }

    /**
     * 删除
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable(name = "id") long id) {

        Item item = itemService.getById(id);
        item.setStatus(Globale.STATS_DELETE);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        log.info("删除成功");
    }

    /**
     * 更新
     *
     * @param item
     */
    @PutMapping
    public String update(@RequestBody Item item) {
        //
        item.setStatus(null);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        log.info("更新成功");
        return "更新成功";
    }


}
